//获取应用实例
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    interaction_hidden: true,
    popupCol: true,
    redeemCol: true, //兑换美酒弹窗
    msg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getUserInfo({
      success: function(res) {
        that.setData({
          userInfo: res.userInfo
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.userStatus();
    this.newpm();
    this.setData({
      msg: '',
      redeemCode: ''
    })
  },
  onHide: function() {
    this.closeRedeemCode();
  },

  //是否有新消息
  newpm: function() {
    var that = this;
    api.POST({
      params: {},
      url: app.globalData.base_url + 'inn/newpm',
      success: function(res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            msgCount: res.data.data.newGoodsMsg / 1 + res.data.data.newInnMsg / 1
          })

        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  //获取个人状态详情
  userStatus: function(e) {
    var that = this;
    api.POST({
      params: "",
      url: app.globalData.base_url + 'user/status',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          app.globalData.userStatus = res.data.data
          if (res.data.data.inn_id) {
            that.setData({
              inn_id: res.data.data.inn_id
            })
          }
          that.setData({
            avatar: res.data.data.avatar,
            nickname: res.data.data.nickname,
            user_flag: res.data.data.user_flag,
            audit_status: res.data.data.audit_status || null,
            unread: res.data.data.unread,
            account_type: res.data.data.account_type
          })
        }
      },
      fail: function(fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },


  myOrder: function(e) {
    var types = e.currentTarget.dataset.types;
    wx.navigateTo({
      url: '/center/pages/myOrder/myOrder?types=' + types
    })
  },
  giftCard: function() {
    wx.navigateTo({
      url: '/play/pages/myGift/myGift'
    })
  },
  moochWine: function() {
    wx.navigateTo({
      url: '/play/pages/moochWine/moochWine'
    })
  },
  pickupRecord: function() {
    wx.navigateTo({
      url: '/center/pages/pickupRecord/pickupRecord'
    })
  },
  coupons: function() {
    wx.navigateTo({
      url: '/center/pages/coupons/coupons'
    })
  },
  afterList: function() {
    wx.navigateTo({
      url: '/center/pages/afterList/afterList'
    })
  },
  myIncome: function() {
    wx.navigateTo({
      url: '/center/pages/myIncome/myIncome'
    })
  },

  contact: function() {
    wx.makePhoneCall({
      phoneNumber: '4000788519'
    })
  },

  bindPhone: function() {
    wx.navigateTo({
      url: '/center/pages/phoneBind/phoneBind'
    })
  },

  interaction: function() {
    this.setData({
      interaction_hidden: !this.data.interaction_hidden
    })
  },
  talk: function() {
    wx.navigateTo({
      url: '/center/pages/writing/writing',
    })
  },
  popupSel: function() {
    this.setData({
      popupCol: !this.data.popupCol
    })
  },

  myFriend: function() {
    wx.navigateTo({
      url: '/play/pages/myFriend/myFriend',
    })
  },
  footprint: function() {
    wx.navigateTo({
      url: '/index/pages/storeFootprint/storeFootprint'
    })
  },
  messageLink: function() {
    wx.navigateTo({
      url: '/agent/pages/message/message'
    })
  },

  shopLink: function() {
    var inn_id = this.data.inn_id
    var account_type = this.data.account_type
    wx.navigateTo({
      url: '/agent/pages/shop/shop?inn_id=' + inn_id,
    })
  },

  removeCache: function() {
    wx.removeStorageSync('accountInfo')
  },

  myShop: function() {
    wx.navigateToMiniProgram({
      appId: 'wx346b5dc88d4606d0',
      path: '',
      extraData: {
        isRedirected: 1
      },
      //develop	开发版 trial	体验版  release	正式版
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

  //打开兑换美酒弹窗
  redeemCode: function() {
    this.setData({
      redeemCol: false
    })
  },
  //关闭兑换美酒弹窗
  closeRedeemCode: function() {
    this.setData({
      redeemCol: true,
      msg: '',
      redeemCode: ''
    })
  },
  //提交兑换美酒
  submitRedeem: function() {
    let code = this.data.redeemCode.replace(/\s*/g, "");
    let that = this
    if (!code) {
      this.setData({
        msg: '请输入兑换码'
      })
    } else {
      that.setData({
        msg: ''
      })
      api.POST({
        params: {
          code: code
        },
        url: app.globalData.base_url + 'giftsarticles/benefitSubmit',
        success: function(res) {
          if (util.isEmpty(res.data.code)) {
            //兑换成功
            that.setData({
              redeemCol: true
            })

            wx.showModal({
              title: '提示',
              content: '兑换成功，礼品已放入您的酒柜',
              confirmText: '我知道了',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/wineNew/wineNew'
                  })
                }
              }
            })

          } else {
            //兑换失败
            that.setData({
              msg: res.data.message
            })
          }
        },
        fail: function(fail) {
          util.showError("网络错误，请检查网络");
        }
      })
    }
  },
  //输入兑换码
  redeemCodeInput: function(e) {
    let value = e.detail.value;
    this.setData({
      redeemCode: value
    })
  }


})